$(function () {
    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var permission_datagrid =  $('#permission_datagrid');

    //数据表格
    $('#permission_datagrid').datagrid({
        url:'/permission/list.do',
        striped:true,
        singleSelect:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',hidden:true,width:100},
            {field:'cb',checkbox:true},
            {field:'name',title:'权限名称',width:100,align:'center'},
            {field:'resource',title:'权限表达式',width:100,align:'center'}
        ]]
    });
    //end

    var cmdObj = {
        //刷新
        reload : function () {
            permission_datagrid.datagrid("reload");
        },
        //删除权限
        remove : function () {
            var row = permission_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的权限","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/permission/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                permission_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //加载权限
        lock : function () {
            $.messager.confirm('温馨提示','是否要加载更新权限？',function(r){
                if (r){
                    $.get("/permission/reload.do",function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','加载成功',function(r){
                                permission_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        }
    }

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end

})//end

